package com.example.motion.service.impl;

import com.example.motion.dao.MotionActivityDateMapper;
import com.example.motion.model.MotionActivityDate;
import com.example.motion.service.IMotionActivityDateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <br>Title:
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@Service
public class MotionActivityDateServiceImpl implements IMotionActivityDateService {

    @Autowired
    private MotionActivityDateMapper motionActivityDateMapper;

    DateFormat dateFormat = new java.text.SimpleDateFormat("yyyy-MM-dd");

    @Override
    public MotionActivityDate getActivityDate() {
        return  motionActivityDateMapper.getActivityDate();
    }

    @Override
    public Map<String, String> getActivityStatus() {
        MotionActivityDate motionActivityDate = motionActivityDateMapper.getActivityDate();
        Date startDate = motionActivityDate.getStartDate();
        Date endDate = motionActivityDate.getEndDate();
        Date now = new Date();
        String status = "0";
        try {
            now = dateFormat.parse(dateFormat.format(now));
            // 活动未开始
            // //返回1 表示now大于startDate ,返回-1表示now小于startDate ,返回0表示日期相等
            if (now.compareTo(startDate) < 0) {
                status = "1";
            }
            // 活动已结束
            else if (now.compareTo(endDate) > 0) {
                status = "2";
            }
            // 活动已结束
            else {
                status = "0";
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Map<String, String> map = new HashMap<>(1);
        map.put("status", status);
        return  map;
    }
}
